import axios from 'axios'
//创建ajax的方法
export default function (url, data = {}, type = 'GET') {
    return new Promise((resolve, reject) => {
        let promise;
        if (type == 'GET') {
            //发送get请求
            let dataStr = '';
            Object.keys(data).forEach(key => {
                // dataStr += key + '=' + data[key] + '&'
                dataStr += '/' + data[key];
            })
            if (dataStr != '') {
                // dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'));
                // url = url + '?' + dataStr;
                url = url + dataStr;
            }
            promise = axios.get(url)

        } else {
            //发送post请求
            promise = axios.post(url, data)
        }
        //成功的回调
        promise.then((result) => {
            resolve(result.data)
        })
            //失败的回调
            .catch((err) => {
                reject(err)
            });
    }
    )

}

